package com.kq.servlet.kqctrl;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.PreloadListener;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.utils.JsonUtil;

public class isDiary extends HttpServlet
{
  public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    response.setCharacterEncoding("UTF-8");
    HtmlPage rootPage = PreloadListener.rootPage;
    List getList = JsonUtil.json2List(request.getParameter("data").toString());
    Map result = new HashMap();
    String html = "";
    String name = PreloadListener.loginInfo.get("name").toString();
    String pswd = PreloadListener.loginInfo.get("pswd").toString();
    String ajaxContent = "$.ajax({url:'http://erp.kanq.com.cn:806/pages/user_login.action',type:'get',dataType:'json',";
    ajaxContent = ajaxContent + "data:{'name':'" + name + "','pswd':'" + pswd + "'},async:false,cache:false,";
    ajaxContent = ajaxContent + "error: function(xhr){$('body').html(JSON.stringify(xhr.responseText));}});";
    rootPage.executeJavaScript(ajaxContent);
    try {
      Thread.sleep(100L);
    }
    catch (Exception localException) {
    }
    html = rootPage.getBody().getTextContent();
    System.out.println(html);
    while (html.indexOf("/system/gateway/index.html?") == -1) {
      try {
        Thread.sleep(100L);
      }
      catch (Exception localException1) {
      }
      html = rootPage.getBody().getTextContent();
    }
    if (html.indexOf("/system/gateway/index.html?") != -1) {
      int n = 0;
      String errorResult = "";
      for (int i = 0; i < getList.size(); i++) {
        html = "";
        String getStr = getList.get(i).toString();
        String sqlStr = getStr.substring(2, getStr.length() - 2);
        System.out.println(sqlStr);
        String insertContent = "";
        insertContent = insertContent + "$.ajax({url:'http://erp.kanq.com.cn:806/pages/ajaxrequest.jsp',type:'post',dataType:'json',";
        insertContent = insertContent + "data:{'data':\"SqlExecute(insert into kanq_diary(contenttext,d_date,d_time,userid,rzlb,bmid,txrq,d_date2,projName) values(" + sqlStr + "))\"},";
        insertContent = insertContent + "error: function(xhr){$('body').html(JSON.stringify(xhr.responseText));}});";

        rootPage.executeJavaScript(insertContent);
        try {
          Thread.sleep(100L);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        html = rootPage.getBody().getTextContent();
        System.out.println(html);
        while (html.indexOf("SQL语句执行成功") == -1) {
          try {
            Thread.sleep(100L);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
          i++;
          if (i > 15) {
            break;
          }
          html = rootPage.getBody().getTextContent();
        }
        if (html.indexOf("SQL语句执行成功") != -1)
          n++;
        else {
          errorResult = errorResult + sqlStr.split(",")[1] + ",";
        }
      }
      result.put("status", "插入" + getList.size() + "条数据，失败" + (getList.size() - n) + "条！");
      if (errorResult != "")
        result.put("errorText", errorResult);
      else {
        result.put("errorText", "");
      }
    }

    System.out.println("html=" + result);
    PrintWriter out = response.getWriter();
    out.write(JsonUtil.stringify(result));
    out.flush();
  }
}